package com.kaizige.realtor.Dao;

import com.kaizige.realtor.Pojo.houseOwner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class houseOwnerDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	// 查询所有的房主
	public List<Map<String,Object>> queryAllOwn(){
		String sql = "select * from house_owner";
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		return maps;
	}
	// 增加房主信息
	public void addOwn(houseOwner ho){
		String sql = "insert into house_owner values (?,?,?,?,?);";
		Object[] args = {ho.getName(), ho.getAddress(), ho.getPhone(),ho.getPassword(),0.0};
		jdbcTemplate.update(sql,args);
	}
	// 按名字查找房主信息
	public Map<String, Object> queryByName(String userName){
		Map<String, Object> map;
		try{
			String sql = "select * from house_owner where name=?";
			map = jdbcTemplate.queryForMap(sql, userName);
		}catch(EmptyResultDataAccessException e){
			return null;
		}
		return map;
	}
	
	// 按名字修改房主信息
	public void updateOwnByName(String userName, String address, String phone){
		String sql = "update house_owner set address=?, phone=? where name=?";
		Object[] args = {address, phone, userName};
		jdbcTemplate.update(sql,args);
	}
	
	// 扣钱
	public void payRent(String name){
		String sql = "update house_owner set balance = balance - 100 where name=?";
		jdbcTemplate.update(sql,name);
	}
}
